#include "ui.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>

#define BUFFER_SIZE 100000

void InfoWindow()//ok,独立
{
    GtkWidget * info_vbox;
    GtkWidget * bt_info;
    if(info_window == NULL){
        info_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_window_set_title(GTK_WINDOW(info_window),"窝窝提示");
        gtk_window_set_position( GTK_WINDOW(info_window),GTK_WIN_POS_CENTER_ALWAYS);
        gtk_widget_set_size_request(GTK_WIDGET(info_window),200,150);

        lb_info = gtk_label_new("测试");
        bt_info = gtk_button_new_with_label("确定");
        info_vbox = gtk_vbox_new(FALSE,10);
        gtk_box_pack_start(GTK_BOX(info_vbox),lb_info,FALSE,FALSE,30);
        gtk_box_pack_start(GTK_BOX(info_vbox),bt_info,FALSE,FALSE,10);

        gtk_container_add(GTK_CONTAINER(info_window),info_vbox);
        g_signal_connect((gpointer)bt_info,"button_press_event",G_CALLBACK(info_event),NULL);
    }
}
void ConfirmWindow()
{
    GtkWidget * info_vbox;
    GtkWidget * info_hbox;
    GtkWidget * bt_ok;
    GtkWidget * bt_refuse;
    GtkWidget * lb_const;

    if(confirm_window == NULL){
        confirm_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_window_set_title(GTK_WINDOW(confirm_window),"窝窝好友提醒");
        gtk_window_set_position( GTK_WINDOW(confirm_window),GTK_WIN_POS_CENTER_ALWAYS);
        gtk_widget_set_size_request(GTK_WIDGET(confirm_window),200,150);

        lb_info_sour = gtk_label_new("");
        lb_const = gtk_label_new("请求添加好友");
        bt_ok = gtk_button_new_with_label("接受");
        bt_refuse = gtk_button_new_with_label("拒绝");
        info_hbox = gtk_hbox_new(FALSE,10);
        info_vbox = gtk_vbox_new(FALSE,10);
        gtk_box_pack_start(GTK_BOX(info_hbox),bt_ok,FALSE,FALSE,20);
        gtk_box_pack_start(GTK_BOX(info_hbox),bt_refuse,FALSE,FALSE,0);
        gtk_box_pack_start(GTK_BOX(info_vbox),lb_info_sour,FALSE,FALSE,10);
        gtk_box_pack_start(GTK_BOX(info_vbox),lb_const,FALSE,FALSE,10);
        gtk_box_pack_start(GTK_BOX(info_vbox),info_hbox,FALSE,FALSE,10);

        gtk_container_add(GTK_CONTAINER(confirm_window),info_vbox);
        g_signal_connect((gpointer)bt_ok,"button_press_event",G_CALLBACK(confirm_addfriend_event),NULL);
        g_signal_connect((gpointer)bt_refuse,"button_press_event",G_CALLBACK(cancel_addfriend_event),NULL);
    }
}

void MainWindow()//独立
{
    if(main_window == NULL){
        GtkWidget * self_vbox;//昵称+个人签名
        GtkWidget * self_hbox;//昵称+个人签名+头像
        GtkWidget * add_image;//添加好友
        GtkWidget * add_event_box;//添加好友-盒子
        GtkWidget * dele_image;//删除好友
        GtkWidget * dele_event_box;//删除好友-盒子
        GtkWidget * manager_hbox;//添加+删除
        GtkWidget * exit_image;//退出
        GtkWidget * exit_event_box;//退出-盒子
        GtkWidget * exit_align;//退出-右对其
        GtkWidget * friend_frame;//好友框架
        GtkWidget * friend_scroll;//好友滚动条

        GtkWidget * main_vbox;//主布局

        GtkWidget * test_label;
        GtkWidget * test_event_box;
        GtkWidget * test_align;

        sem_init(&sign_lb_lock,0,1);
        sem_init(&sign_text_view,0,1);

        main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

        gtk_window_set_title(GTK_WINDOW(main_window),"窝窝2014");
        gtk_window_set_position( GTK_WINDOW(main_window),GTK_WIN_POS_CENTER_ALWAYS);
        gtk_widget_set_size_request(GTK_WIDGET(main_window),280,600);

        head_image = gtk_image_new_from_file("./images/head.png");
        //head_image = gtk_image_new();
        self_label = gtk_label_new("初始标签");
        sign_label = gtk_label_new("个人签名展位符");

        self_vbox = gtk_vbox_new(FALSE,10);
        self_hbox = gtk_hbox_new(FALSE,10);
        gtk_box_pack_start(GTK_BOX(self_vbox),self_label,FALSE,FALSE,10);
        gtk_box_pack_start(GTK_BOX(self_vbox),sign_label,FALSE,FALSE,10);

        gtk_box_pack_start(GTK_BOX(self_hbox),head_image,FALSE,FALSE,10);
        gtk_box_pack_start(GTK_BOX(self_hbox),self_vbox,FALSE,FALSE,10);

        friend_frame = gtk_frame_new("我的好友");
        gtk_widget_set_size_request(friend_frame,200,300);

        friend_scroll = gtk_scrolled_window_new(NULL,NULL);
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(friend_scroll),GTK_POLICY_AUTOMATIC,GTK_POLICY_ALWAYS);
        friend_vbox = gtk_vbox_new(FALSE,0);

        //以下会专门往里加好友
        //test_label = gtk_label_new("测试好友");
        //test_align = gtk_alignment_new(0,0,0,0);
        //test_event_box = gtk_event_box_new();

        //gtk_widget_set_events (test_event_box,GDK_BUTTON_PRESS_MASK);
        //gtk_container_add(GTK_CONTAINER(test_event_box),test_label);
        //gtk_container_add(GTK_CONTAINER(test_align),test_event_box);
        //gtk_box_pack_start(GTK_BOX(friend_vbox),test_align,FALSE,FALSE,10);

        gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(friend_scroll),friend_vbox);
        gtk_container_add(GTK_CONTAINER(friend_frame),friend_scroll);

        add_image = gtk_image_new_from_file("./images/add.png");
        //add_image = gtk_image_new();
        add_event_box = gtk_event_box_new();
        dele_image = gtk_image_new_from_file("./images/dele.png");
        //dele_image = gtk_image_new();
        dele_event_box = gtk_event_box_new();
        gtk_container_add(GTK_CONTAINER(add_event_box),add_image);
        gtk_container_add(GTK_CONTAINER(dele_event_box),dele_image);


        manager_hbox = gtk_hbox_new(FALSE,10);
        gtk_box_pack_start(GTK_BOX(manager_hbox),add_event_box,FALSE,FALSE,10);
        gtk_box_pack_start(GTK_BOX(manager_hbox),dele_event_box,FALSE,FALSE,10);

        exit_image = gtk_image_new_from_file("./images/exit.ico");
        exit_event_box =  gtk_event_box_new();
        exit_align = gtk_alignment_new(1,1,0,0);
        gtk_container_add(GTK_CONTAINER(exit_event_box),exit_image);
        gtk_container_add(GTK_CONTAINER(exit_align),exit_event_box);


        main_vbox = gtk_vbox_new(FALSE,0);
        gtk_box_pack_start(GTK_BOX(main_vbox),self_hbox,FALSE,FALSE,10);
        gtk_box_pack_start(GTK_BOX(main_vbox),friend_frame,FALSE,FALSE,10);
        gtk_box_pack_start(GTK_BOX(main_vbox),manager_hbox,FALSE,FALSE,10);
        gtk_box_pack_start(GTK_BOX(main_vbox),exit_align,FALSE,FALSE,10);

        gtk_container_add(GTK_CONTAINER(main_window),main_vbox);

        g_signal_connect((gpointer)exit_event_box,"button_press_event",G_CALLBACK(exit_event),NULL);
        g_signal_connect((gpointer)main_window,"delete_event",G_CALLBACK(exit_event),NULL);
        //测试其他按钮是否有问题-------------------最后调整
        g_signal_connect((gpointer)add_event_box,"button_press_event",G_CALLBACK(show_add_event),NULL);
        g_signal_connect((gpointer)dele_event_box,"button_press_event",G_CALLBACK(show_delete_event),NULL);
        //g_signal_connect((gpointer)test_event_box,"button_press_event",G_CALLBACK(creat_chat_event),NULL);
    }
}

void AddWindow()
{
    if(add_window == NULL){
        GtkWidget * label;
        GtkWidget * bt_ok;
        GtkWidget * bt_cancel;
        GtkWidget * et_friend_id;
        GtkWidget * hbox_add;
        GtkWidget * vbox_add;
        //GdkColor  * color = (GdkColor*)malloc(sizeof(GdkColor));
        add_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        //gtk_window_set_title(GTK_WINDOW(add_window),"添加好友");
        gtk_window_set_position(GTK_WINDOW(add_window),GTK_WIN_POS_CENTER_ALWAYS);
        //gdk_color_parse ("blue", color);
        //gdk_window_set_background(GDK_WINDOW(add_window),color);
        label = gtk_label_new("添加好友");
        bt_ok = gtk_button_new_with_label("确定");
        bt_cancel = gtk_button_new_with_label("取消");

        et_friend_id = gtk_entry_new();

        hbox_add = gtk_hbox_new(FALSE,10);
        vbox_add = gtk_vbox_new(FALSE,10);
        gtk_box_pack_start(GTK_BOX(hbox_add),bt_ok,FALSE,FALSE,10);
        gtk_box_pack_start(GTK_BOX(hbox_add),bt_cancel,FALSE,FALSE,10);
        gtk_box_pack_start(GTK_BOX(vbox_add),label,FALSE,FALSE,10);
        gtk_box_pack_start(GTK_BOX(vbox_add),et_friend_id,FALSE,FALSE,10);
        gtk_box_pack_start(GTK_BOX(vbox_add),hbox_add,FALSE,FALSE,10);
        gtk_container_add(GTK_CONTAINER(add_window),vbox_add);

        g_signal_connect((gpointer)bt_ok,"button_press_event",G_CALLBACK(ok_add_event),et_friend_id);//确定按钮事件
        g_signal_connect((gpointer)bt_cancel,"button_press_event",G_CALLBACK(cancel_add_event),et_friend_id);//取消按钮事件
        //g_signal_connect((gpointer)add_window,"delete_event",G_CALLBACK(cancel_add_event),NULL);
    }
}

void DeleWindow()
{
    if(dele_window == NULL){
        GtkWidget * label;
        GtkWidget * bt_ok;
        GtkWidget * bt_cancel;
        GtkWidget * et_friend_id;
        GtkWidget * hbox_add;
        GtkWidget * vbox_add;

        dele_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        //gtk_window_set_title(GTK_WINDOW(dele_window),"删除好友");
        gtk_window_set_position(GTK_WINDOW(dele_window),GTK_WIN_POS_CENTER_ALWAYS);

        label = gtk_label_new("删除好友");
        bt_ok = gtk_button_new_with_label("确定");
        bt_cancel = gtk_button_new_with_label("取消");

        et_friend_id = gtk_entry_new();

        hbox_add = gtk_hbox_new(FALSE,10);
        vbox_add = gtk_vbox_new(FALSE,10);
        gtk_box_pack_start(GTK_BOX(hbox_add),bt_ok,FALSE,FALSE,10);
        gtk_box_pack_start(GTK_BOX(hbox_add),bt_cancel,FALSE,FALSE,10);
        gtk_box_pack_start(GTK_BOX(vbox_add),label,FALSE,FALSE,10);
        gtk_box_pack_start(GTK_BOX(vbox_add),et_friend_id,FALSE,FALSE,10);
        gtk_box_pack_start(GTK_BOX(vbox_add),hbox_add,FALSE,FALSE,10);
        gtk_container_add(GTK_CONTAINER(dele_window),vbox_add);

        g_signal_connect((gpointer)bt_ok,"button_press_event",G_CALLBACK(ok_dele_event),et_friend_id);//确定按钮事件
        g_signal_connect((gpointer)bt_cancel,"button_press_event",G_CALLBACK(cancel_dele_event),et_friend_id);//取消按钮事件
    }
}

void LoginWindow()//ok，独立
{
    if(login_window == NULL){
        GtkWidget * lb_id;
        GtkWidget * lb_pass;
        GtkWidget * bt_login;
        GtkWidget * bt_regi;
        GtkWidget * table;

        login_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_window_set_title(GTK_WINDOW(login_window),"喔喔登录");
        gtk_window_set_position( GTK_WINDOW(login_window),GTK_WIN_POS_CENTER_ALWAYS);

        lb_id = gtk_label_new("帐号：");
        lb_pass = gtk_label_new("密码：");
        et_id = gtk_entry_new();
        et_pass = gtk_entry_new();
        gtk_entry_set_visibility(et_pass,FALSE);

        bt_login = gtk_button_new_with_label("登录");
        bt_regi = gtk_button_new_with_label("注册");
        table = gtk_table_new(3,3,TRUE);
        gtk_table_set_row_spacings(GTK_TABLE(table),10);
        gtk_table_set_col_spacings(GTK_TABLE(table),10);
        /*
    gtk_table_attach_defaults(GTK_TABLE(table),lb_id,0,1,0,1);
    gtk_table_attach_defaults(GTK_TABLE(table),et_id,1,3,0,1);
    gtk_table_attach_defaults(GTK_TABLE(table),lb_pass,0,1,1,2);
    gtk_table_attach_defaults(GTK_TABLE(table),et_pass,1,3,1,2);
    gtk_table_attach_defaults(GTK_TABLE(table),bt_login,0,1,2,3);
    gtk_table_attach_defaults(GTK_TABLE(table),bt_regi,1,3,2,3);
    */
#ifndef set
#define set 0,0,10,10
#endif
        gtk_table_attach(GTK_TABLE(table),lb_id,0,1,0,1,set);
        gtk_table_attach(GTK_TABLE(table),et_id,1,3,0,1,set);
        gtk_table_attach(GTK_TABLE(table),lb_pass,0,1,1,2,set);
        gtk_table_attach(GTK_TABLE(table),et_pass,1,3,1,2,set);
        gtk_table_attach(GTK_TABLE(table),bt_login,0,1,2,3,set);
        gtk_table_attach(GTK_TABLE(table),bt_regi,1,3,2,3,set);

        gtk_container_add(GTK_CONTAINER(login_window),table);

        g_signal_connect((gpointer)login_window,"delete_event",G_CALLBACK(exit_event),NULL);
        g_signal_connect((gpointer)bt_login,"button_press_event",G_CALLBACK(login_event),NULL);
        g_signal_connect((gpointer)bt_regi,"button_press_event",G_CALLBACK(regi_event),NULL);
    }

}

void ChatWindow(GtkWidget ** from_window,GtkWidget ** from_text_view)//ok，独立
{
    GtkWidget * to_window;
    //GtkWidget * to_text_view;
    GtkWidget * reci_text_view;
    GtkWidget * send_text_view;
    GtkWidget * occupy_image;
    GtkWidget * occupy_event_box;
    GtkWidget * reci_scroll;
    GtkWidget * send_scroll;
    GtkWidget * send_image;
    GtkWidget * send_file_image;
    GtkWidget * exit_image;
    GtkWidget * send_event_box,* send_align;
    GtkWidget * send_file_event_box,* send_file_align;
    GtkWidget * exit_event_box, * exit_align;
    GtkWidget * up_hbox;
    GtkWidget * down_hbox;
    GtkWidget * down_vbox;
    GtkWidget * main_vbox;

    to_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    char *s;
    gtk_label_get(GTK_LABEL(*from_text_view),&s);
    char str[20]={'\0'};
    sprintf(str,"与%s的聊天",s);
    gtk_window_set_title(GTK_WINDOW(to_window),str);
    gtk_window_set_position( GTK_WINDOW(to_window),GTK_WIN_POS_CENTER_ALWAYS);
    //gtk_widget_set_size_request(GTK_WIDGET(chat_window),300,150);

    occupy_image = gtk_image_new_from_file("./images/occupy.ico");
    reci_text_view = gtk_text_view_new();
    gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(reci_text_view),FALSE);//设置光标不可见
    //gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW(reci_text_view),GTK_WRAP_WORD);自动换行？？不能用
    gtk_widget_set_size_request(GTK_WIDGET(reci_text_view),400,200);
    gtk_text_view_set_editable(GTK_TEXT_VIEW(reci_text_view),FALSE);
    reci_scroll = gtk_scrolled_window_new(NULL,NULL);
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(reci_scroll),GTK_POLICY_AUTOMATIC,GTK_POLICY_ALWAYS);
    gtk_container_add(GTK_CONTAINER(reci_scroll),reci_text_view);
    up_hbox = gtk_hbox_new(FALSE,10);

    gtk_box_pack_start(GTK_BOX(up_hbox),reci_scroll,FALSE,FALSE,10);
    gtk_box_pack_start(GTK_BOX(up_hbox),occupy_image,FALSE,FALSE,10);

    send_image = gtk_image_new_from_file("./images/send.ico");
    send_event_box = gtk_event_box_new();
    send_align = gtk_alignment_new(1,1,0,0);
    gtk_container_add(GTK_CONTAINER(send_event_box),send_image);
    gtk_container_add(GTK_CONTAINER(send_align),send_event_box);


    send_file_image = gtk_image_new_from_file("./images/sendfile.ico");
    send_file_event_box = gtk_event_box_new();
    send_file_align = gtk_alignment_new(1,1,0,0);
    gtk_container_add(GTK_CONTAINER(send_file_event_box),send_file_image);
    gtk_container_add(GTK_CONTAINER(send_file_align),send_file_event_box);


    exit_image = gtk_image_new_from_file("./images/exit_chat.ico");
    exit_event_box = gtk_event_box_new();
    exit_align = gtk_alignment_new(1,1,0,0);
    gtk_container_add(GTK_CONTAINER(exit_event_box),exit_image);
    gtk_container_add(GTK_CONTAINER(exit_align),exit_event_box);


    down_vbox = gtk_vbox_new(FALSE,10);
    gtk_box_pack_start(GTK_BOX(down_vbox),send_align,FALSE,FALSE,0);
    gtk_box_pack_start(GTK_BOX(down_vbox),send_file_align,FALSE,FALSE,0);
    gtk_box_pack_start(GTK_BOX(down_vbox),exit_align,FALSE,FALSE,0);

    down_hbox = gtk_hbox_new(FALSE,10);
    send_text_view = gtk_text_view_new();
    gtk_widget_set_size_request(GTK_WIDGET(send_text_view),400,50);
    send_scroll = gtk_scrolled_window_new(NULL,NULL);
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(send_scroll),GTK_POLICY_AUTOMATIC,GTK_POLICY_ALWAYS);
    gtk_container_add(GTK_CONTAINER(send_scroll),send_text_view);

    gtk_box_pack_start(GTK_BOX(down_hbox),send_scroll,FALSE,FALSE,10);
    gtk_box_pack_start(GTK_BOX(down_hbox),down_vbox,FALSE,FALSE,10);

    main_vbox = gtk_vbox_new(FALSE,10);
    gtk_box_pack_start(GTK_BOX(main_vbox),up_hbox,FALSE,FALSE,10);
    gtk_box_pack_start(GTK_BOX(main_vbox),down_hbox,FALSE,FALSE,10);

    gtk_container_add(GTK_CONTAINER(to_window),main_vbox);
    g_signal_connect((gpointer)to_window,"delete_event",G_CALLBACK(hide_chat_window_event),to_window);//叉事件
    g_signal_connect((gpointer)exit_event_box,"button_press_event",G_CALLBACK(hide_chat_window_event),to_window);//退出事件
    //把id封装，lb封装。发送
    FriendId * data = (FriendId *)malloc(sizeof(FriendId));
    data->lb_id = to_window;//够了
    data->text_view = send_text_view;
    g_signal_connect((gpointer)send_event_box,"button_press_event",G_CALLBACK(send_msg_event),data);//发送按钮事件
    g_signal_connect((gpointer)send_file_event_box,"button_press_event",G_CALLBACK(send_file_event),data);
    (*from_window) = to_window;
    (*from_text_view) = reci_text_view;
}

void AddMessage(int id,const char *msg)
{
    char str[100];
    GtkTextBuffer *buffer;
    GtkTextIter start,end;
    GtkWidget * text_view = id_to_textview(&map,id);
    buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_view));
    //lbnum = id_to_idnum(&map,pkg.destination);暂时不设置标签
    gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(buffer),&start,&end);
    sprintf(str,"来信：\n%s\n",msg);
    gtk_text_buffer_insert(buffer,&end,str,-1);

    if(gtk_widget_get_sensitive(GTK_WIDGET(id_to_window(&map,id))) == TRUE){
        //if(gtk_window_is_active(GTK_WINDOW(id_to_window(&map,id))) == TRUE){
        printf("已显示\n");
    }
    else
        printf("未显示\n");
}

void send_msg_event(GtkWidget *widget, GdkEvent *event, gpointer data)//一个问题：这之前可以没有被插入字典
{
    GtkWidget *  window = ((FriendId *)data)->lb_id;
    GtkWidget *  send_textview = ((FriendId *)data)->text_view;//发送缓冲
    if(window_to_id(&map,window) == -1)//还没有加入字典，此概率极小！！
        return ;
    const int id = window_to_id(&map,window);
    GtkWidget *  reci_textview = id_to_textview(&map,id);
    GtkTextBuffer *  send_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(send_textview));
    GtkTextBuffer *  reci_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(reci_textview));
    GtkTextIter sstart,send;
    GtkTextIter tstart,tend;

    gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(send_buffer),&sstart,&send);/*获得缓冲区开始和结束位置的Iter*/
    gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(reci_buffer),&tstart,&tend);

    const char * s = gtk_text_buffer_get_text(GTK_TEXT_BUFFER(send_buffer),&sstart,&send,FALSE);/*获得文本框缓冲区文本*/
    gboolean set_or_notset = FALSE;//tag是否设置过了
    if(set_or_notset == FALSE){
        gtk_text_buffer_create_tag(reci_buffer, "gap",
                                   "pixels_above_lines", 30, NULL);
        gtk_text_buffer_create_tag(reci_buffer, "lmarg",
                                   "left_margin", 5, NULL);
        gtk_text_buffer_create_tag(reci_buffer, "blue_fg",
                                   "foreground", "blue", NULL);
        gtk_text_buffer_create_tag(reci_buffer, "gray_bg",
                                   "background", "gray", NULL);
        gtk_text_buffer_create_tag(reci_buffer, "italic",
                                   "style", PANGO_STYLE_ITALIC, NULL);
        gtk_text_buffer_create_tag(reci_buffer, "bold",
                                   "weight", PANGO_WEIGHT_BOLD, NULL);
        set_or_notset = TRUE;
    }
    gchar str[200];
    struct Package pkg;
    pkg.type = 4;
    pkg.destination = id;
    sprintf(pkg.msg,"%s",s);//这种可以自动加\0结束符号
    if(strcmp(s,"") == 0 || strlen(s) > 180)//消息过长
        return ;
    write(sockfd,&pkg,sizeof(pkg));
    sprintf(str,"发送：\n%s\n",s);
    sem_wait(&sign_text_view);
    gtk_text_buffer_insert_with_tags_by_name(reci_buffer,&tend,str,-1,"blue_fg","lmarg","style",NULL);
    sem_post(&sign_text_view);
    //gtk_text_buffer_set_text(reci_buffer,str,-1);
    //gtk_text_buffer_insert(reci_buffer,&tstart,str,-1);
    //gtk_text_buffer_create_tag(reci_buffer, "blue_fg", "foreground", "blue", NULL);
    gtk_text_buffer_set_text(send_buffer,"",-1);
}

void hide_chat_window_event(GtkWidget *widget,GdkEvent *event,gpointer data)
{
    gtk_widget_hide_all(GTK_WIDGET(((GtkWidget *)data)));
}

void info_event(GtkWidget * widget, gpointer data)//ok
{
    gtk_widget_hide_all(info_window);
}

void exit_event(GtkWidget * widget, gpointer data)//ok
{
    struct Package pkg;
    pkg.type = 3;
    write(sockfd,&pkg,sizeof(pkg));
    exit_code = TRUE;
    close(sockfd);
    sleep(1);
    gtk_main_quit();
}

void regi_event(GtkWidget * widget , GdkEvent *event, gpointer data)//ok
{
    struct Package pkg;
    GtkWidget * msg;
    if(connect_code == FALSE){
        msg = gtk_message_dialog_new(
                    NULL,
                    GTK_DIALOG_DESTROY_WITH_PARENT,
                    GTK_MESSAGE_ERROR,
                    GTK_BUTTONS_OK,
                    "连接服务器失败！");
        gtk_dialog_run(GTK_DIALOG(msg));
        gtk_widget_destroy(msg);
        exit_event(widget,NULL);
        return ;
    }
    {
        pkg.type = 1;
    }
    write(sockfd,&pkg,sizeof(pkg));
}

void creat_chat_event(GtkWidget *widget, GdkEvent *event, gpointer data)//ok
{

    int id = idnum_to_id(&map,((FriendId *)data)->lb_msg);//为lbnum计数标签
    if(id_to_window(&map,id) != NULL){
        //GtkWidget * win = find_window(&thread_id_window,id);
        GtkWidget *win = id_to_window(&map,id);
        sem_wait(&sign_lb_lock);//改变标签信息
        gtk_label_set_label(GTK_LABEL(((FriendId *)data)->lb_msg),"");//或者写成另外一种形式通过id获取
        sem_post(&sign_lb_lock);
        gtk_widget_show_all(GTK_WIDGET(win));
        return ;
    }
    GtkWidget * window;
    GtkWidget * text_view = ((FriendId *)data)->lb_id;//复用，把标签传进去，用于窗口名称
    ChatWindow(&window,&text_view);//把窗口初始化，聊天窗口和接收消息框，上框
    ((FriendId *)data)->text_view = text_view;
    GThread * thread = NULL;
    /*if(( thread = g_thread_create(chat_msg_handle,data,FALSE,NULL)) == NULL){//消息处理线程接收，id，上textview，idnum
        printf("deadliness error!");
        exit(0);
    }*/
    //add(&thread_id_window,thread,window);
    //add_thread(&thread_id_window,thread,id,window);
    add_map_again(&map,id,window,text_view,thread);//更新值

    sem_wait(&sign_lb_lock);
    gtk_label_set_label(GTK_LABEL(((FriendId *)data)->lb_msg),"");
    sem_post(&sign_lb_lock);

    gtk_widget_show_all(GTK_WIDGET(window));

}


void login_event(GtkWidget * widget, GdkEvent *event, gpointer data)//ok
{
    GtkWidget * msg;
    if(connect_code == FALSE){//服务器连接失败，退出程序
        msg = gtk_message_dialog_new(
                    NULL,
                    GTK_DIALOG_DESTROY_WITH_PARENT,
                    GTK_MESSAGE_ERROR,
                    GTK_BUTTONS_OK,
                    "连接服务器失败！");
        gtk_dialog_run(GTK_DIALOG(msg));
        gtk_widget_destroy(msg);
        exit_event(widget,NULL);//传入值并不使用widget
        return ;
    }
    if(ini_thread_code == FALSE){           //最初值为FALSE，保证线程队列仅被创建一次，下面会把该值设为TRUE
        init_msg(&cqueue);                  //初始化：消息队列
        init_map(&map);

        g_thread_create(c_handle_parse,NULL,FALSE,NULL);
        g_thread_create(c_handle_request,NULL,FALSE,NULL);
        //int err = pthread_create(&p_h_request,NULL,c_handle_request,NULL);//读取消息队列
        ini_thread_code = TRUE;
    }

    struct Package pkg;
    {
        pkg.type = 2;
        sscanf(gtk_entry_get_text(GTK_ENTRY(et_id)),"%d",&pkg.destination);
        strcpy(pkg.msg,gtk_entry_get_text(GTK_ENTRY(et_pass)));
    }
    write(sockfd,&pkg,sizeof(pkg));
}

void test_event(GtkWidget * widget,GdkEvent * event,FriendId * data)
{
    //gdk_threads_enter();
    //gtk_widget_hide_all(GTK_WIDGET(info_window));为什么不需要呢？难道因为在同一个线程里面？
    //char s[20] = {'\0' };
    char *s;
    gtk_label_get(GTK_LABEL(data->lb_id),&s);
    //sprintf(s,"%s",data->lb_msg);
    gtk_label_set_label(GTK_LABEL(lb_info),s);
    gtk_widget_show_all(GTK_WIDGET(info_window));
    //gdk_threads_leave();
}
void show_add_event(GtkWidget *widget, GdkEvent *event, gpointer data)
{
    gtk_widget_show_all(GTK_WIDGET(add_window));
}

void ok_add_event(GtkWidget *widget, GdkEvent *event, gpointer data)
{
    struct Package pkg;
    GtkWidget * entry = (GtkWidget *)data;
    pkg.type = 5;
    sscanf(gtk_entry_get_text(GTK_ENTRY(entry)),"%d",&pkg.destination);
    printf("add friend id %d\n",pkg.destination);
    write(sockfd,&pkg,sizeof(pkg));
    gtk_entry_set_text(GTK_ENTRY(entry),"");
    gtk_widget_hide_all(GTK_WIDGET(add_window));
}

void cancel_add_event(GtkWidget *widget,gpointer data)
{
    GtkWidget * entry = (GtkWidget*)data;
    gtk_entry_set_text(GTK_ENTRY(entry),"");
    gtk_widget_hide_all(GTK_WIDGET(add_window));
}

void show_delete_event(GtkWidget *widget,gpointer data)
{
    gtk_widget_show_all(GTK_WIDGET(dele_window));
}

void ok_dele_event(GtkWidget *widget,gpointer data)
{
    struct Package pkg;
    GtkWidget * entry = (GtkWidget *)data;
    pkg.type = 6;
    sscanf(gtk_entry_get_text(GTK_ENTRY(entry)),"%d",&pkg.destination);
    write(sockfd,&pkg,sizeof(pkg));
    gtk_entry_set_text(GTK_ENTRY(entry),"");
    gtk_widget_hide_all(GTK_WIDGET(dele_window));
}

void cancel_dele_event(GtkWidget *widget,gpointer data)
{
    GtkWidget * entry = (GtkWidget*)data;
    gtk_entry_set_text(GTK_ENTRY(entry),"");
    gtk_widget_hide_all(GTK_WIDGET(dele_window));
}

void confirm_addfriend_event(GtkWidget *widget,gpointer data)
{
    struct Package pkg;
    pkg.type = 8;
    sscanf(gtk_label_get_text(GTK_LABEL(lb_info_sour)),"%d",&pkg.destination);
    printf("%d",10);
    write(sockfd,&pkg,sizeof(pkg));
    gtk_widget_hide_all(GTK_WIDGET(confirm_window));

}

void cancel_addfriend_event(GtkWidget *widget, gpointer data)
{
    gtk_widget_hide_all(GTK_WIDGET(confirm_window));
}

void send_file_event(GtkWidget *widget, GdkEvent *event, gpointer data)
{
    printf("ok\n");
    static GtkWidget * file = NULL;
    if(file == NULL)
    {
        file = gtk_file_selection_new("发送文件");
        ((FriendId*)data)->data = file;
        //确定、取消按钮事件
        g_signal_connect((gpointer)(GTK_FILE_SELECTION(file)->ok_button),"button_press_event",G_CALLBACK(send_file),data);
        g_signal_connect((gpointer)(GTK_FILE_SELECTION(file)->cancel_button),"button_press_event",G_CALLBACK(cancel_file),data);
    }
    gtk_widget_show_all(GTK_WIDGET(file));
    printf("_data : %p\n",data);
    printf("_file: %p\n",file);
}
void cancel_file(GtkWidget *widget, GdkEvent *event, gpointer data)
{
    printf("widget:%p\n",widget);
    printf("event%p\n",event);
    printf("data:%p\n",data);
    //gtk_widget_hide_all(GTK_WIDGET(((GtkWidget *)data)));
    gtk_widget_hide_all((GtkWidget *)(((FriendId *)data)->data));
}

void send_file(GtkWidget *widget, GdkEvent *event, gpointer data)
{
    printf("widget:%p\n",widget);
    printf("event%p\n",event);
    printf("data:%p\n",data);
    printf("file:%p\n",((FriendId *)data)->data);
    const char * filename = gtk_file_selection_get_filename (GTK_FILE_SELECTION (((FriendId*)data)->data));
    printf ("%s\n",filename );
    gtk_widget_hide_all((GtkWidget *)(((FriendId *)data)->data));

    struct Package pkg;
    //构建指令包
    {
        pkg.type = 10;
        pkg.destination = window_to_id(&map,((FriendId *)data)->lb_id);
        pkg.sock = sockfd;
        bzero(pkg.other,sizeof(pkg.other));
        sprintf(pkg.other,"%s",filename);//文件名
    }
    printf("id %d\n",window_to_id(&map,((FriendId *)data)->lb_id));
    write(sockfd,&pkg,sizeof(pkg));
#if 0
    FILE * fp = fopen(filename,"rb");
    if(fp == NULL)
    {
        printf("打开失败\n");
        return;
    }
    char buffer[BUFFER_SIZE];
    bzero(buffer,BUFFER_SIZE);
    int length = fread(buffer,sizeof(char),BUFFER_SIZE,fp);
    printf("文件大小%d\n",length);
    close(fp);
#endif
}
